<?php

namespace app\api\controller\paotui;

use addons\epay\library\Service;
use app\common\controller\Api;
use think\Exception;

use app\admin\model\paotui\Config;
use app\admin\model\paotui\TemplateIds;
use app\admin\model\paotui\Third;
use EasyWeChat\Factory;

/**
 * 接口
 */
class Block extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function notifyx()
    {
        $paytype = "wechat";
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        try {
            $out_trade_no = $data['out_trade_no'];
            $order_no = explode("_",$out_trade_no);

            \addons\paotui\service\Post::pay_success($order_no[0],$data['transaction_id']);
        } catch (Exception $e) {

        }
        echo $pay->success();
    }

    public function test(){
        \addons\paotui\service\Post::pay_success("4236307466289158789","111");
    }

    public function test1(){
		echo 111;
		$configModel = new Config();
		$config = $configModel->where(['group'=>'mini'])->column("name,value");

		$options = [
			'app_id'   => $config['mini_appid'],
			'secret'   => $config['mini_appsecret'],
			'log' => [
				'default' => 'prod', // 默认使用的 channel，生产环境可以改为下面的 prod
				'channels' => [
					// 测试环境
					'dev' => [
						'driver' => 'single',
						'path' => '/tmp/logs/easywechat.log',
						'level' => 'debug',
					],
					// 生产环境
					'prod' => [
						'driver' => 'daily',
						'path' => '/tmp/logs/easywechat.log',
						'level' => 'error',
					],
				],
			],
		];

		$app = Factory::miniProgram($options);
// {"touser":"oHboU5FQR374EO1AuuZ5NZzaGdcU","template_id":"","page":"pages\/voucher\/index?order_no=4340040484664647077","lang":"zh_CN","data":{"character_string1":"4340040484664647077","time2":{"value":"2023-12-15 18:18:07"},"thing3":{"value":" "}}}
		try {
			$mg=[
				'touser'=>'oFQXA63wXiEFGrGbqsF5iQlAGoP4',
				'template_id'=>'7rzOpwdurGRjGRlMb3uDyG2oEKRYQ373K5Co1xL-9VE',
				'page'=>'pages\/index\/index',
				'lang'=>'zh_CN',
				'data'=>[
					'thing1'=>'111'
				]
			];
			p($mg);
			$result = $app->subscribe_message->send($mg);
			p($result);
		}catch (\EasyWeChat\Kernel\Exceptions\Exception $e){
			$errmsg = $e->getMessage();
			p($errmsg);
		}
    }

}
